package vip.xiaonuo.issued.impl;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.pojo.httpclient.ReturnJson;
import vip.xiaonuo.core.util.IpAddressUtil;
import vip.xiaonuo.facedoor.iservice.IFaceDoorDtoService;
import vip.xiaonuo.facedoor.iservice.issulog.DeviceLogService;
import vip.xiaonuo.facedoor.modular.devicebase.entity.DeviceBase;
import vip.xiaonuo.facedoor.modular.devicebase.service.DeviceBaseService;
import vip.xiaonuo.facedoor.rule.RuleConstant;
import vip.xiaonuo.facedoor.vo.dewo.AddPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeletePersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.DeviceStateResponse;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonRequest;
import vip.xiaonuo.facedoor.vo.dewo.GetPersonResponse;
import vip.xiaonuo.facedoor.vo.dewo.UpdatePersonRequest;
import vip.xiaonuo.facedoor.vo.haiqing.BaseResponse;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetDoorCondition;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetGeneralParamRequest;
import vip.xiaonuo.facedoor.vo.haiqing.GetAndSetSoundRequest;
import vip.xiaonuo.facedoor.vo.haiqing.OpenDoorRequest;
import vip.xiaonuo.facedoor.vo.haiqing.RebootDeviceRequest;
import vip.xiaonuo.facedoor.vo.haiqing.RequestHeader;
import vip.xiaonuo.facedoor.vo.haiqing.SetFactoryDefaultRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetSubscribeRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetSysTimeRequest;
import vip.xiaonuo.facedoor.vo.haiqing.SetUserPwdRequest;
import vip.xiaonuo.issued.inter.MqttGateway;
import vip.xiaonuo.issued.bean.CMD;
import vip.xiaonuo.issued.bean.Topic;
import vip.xiaonuo.issued.util.SnowIdGen;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Map;

@Service(value = RuleConstant.DW + RuleConstant.FACEDOOR_DTO)
public class MqttFaceDoorDtoServiceImpl implements IFaceDoorDtoService {

    @Resource
    private MqttGateway gateway;
    @Autowired
    private DeviceLogService logService;

    @Autowired
    private DeviceBaseService deviceBaseService;


    @Override
    public DeviceStateResponse getDeviceState(String deviceId) throws Exception {
//        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        Map<String, Object> baseMap = SnowIdGen.getBaseMap();
        baseMap.put("deviceId", Long.parseLong(deviceId));
        baseMap.put(CMD.CMD, CMD.DEVICE_STATE);
        String req = JSON.toJSONString(baseMap);
        gateway.sendToMqtt(MessageFormat.format(Topic.DEVICE_TOPIC, deviceId), 1, req);
//        logService.addDeviceLog(deviceBase.getName(), req, CMD.REBOOT_DEVICE, (Long) baseMap.get(CMD.SERIAL_NUMBER));
        return null;
    }

    @Override
    public ReturnJson deletePerson(String deviceId, DeletePersonRequest deletePersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        Map<String, Object> delMap = SnowIdGen.getBaseMap();
        delMap.put("deviceId", Long.parseLong(deviceId));
        delMap.put(CMD.CMD, CMD.DELETE_PERSON);
        if (StringUtils.isNotBlank(deletePersonRequest.getPersonnelId())) {
            delMap.put("personnelId", deletePersonRequest.getPersonnelId());
        }
        delMap.put("memberOrVisitorDeviceId", deletePersonRequest.getMemberOrVisitorDeviceId());
        delMap.put("memberType", deletePersonRequest.getMemberType());
        String req = JSON.toJSONString(delMap);
        gateway.sendToMqtt(MessageFormat.format(Topic.PERSON_TOPIC, deviceId), 1, req);
        logService.addDeviceLog(deletePersonRequest.getMemberName(), deviceBase.getName(), req, CMD.DELETE_PERSON, delMap.get(CMD.SERIAL_NUMBER).toString());
        return null;
    }

    @Override
    public ReturnJson deleteAllPerson(String deviceId, DeletePersonRequest deletePersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        Map<String, Object> delMap = SnowIdGen.getBaseMap();
        delMap.put("deviceId", Long.parseLong(deviceId));
        delMap.put(CMD.CMD, CMD.DELETE_ALL_PERSON);
        if (StringUtils.isNotBlank(deletePersonRequest.getDeleteAll())) {
            delMap.put("deleteAll", deletePersonRequest.getDeleteAll());
        }
        String req = JSON.toJSONString(delMap);
        gateway.sendToMqtt(MessageFormat.format(Topic.PERSON_TOPIC, deviceId), 1, req);
        logService.addDeviceLog("", deviceBase.getName(), req, CMD.DELETE_ALL_PERSON, delMap.get(CMD.SERIAL_NUMBER).toString());
        return null;
    }


    @Override
    public ReturnJson addPerson(String deviceId, AddPersonRequest addPersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        Map<String, Object> param = SnowIdGen.getBaseMap();
        param.put("key", "ABCD1234");
        param.put("personnelId", addPersonRequest.getPersonnelId());
        param.put("name", addPersonRequest.getName());
        if (StringUtils.isNotBlank(addPersonRequest.getIdCardNum())) {
            param.put("idCardNum", addPersonRequest.getIdCardNum());
        }
        param.put(CMD.CMD, CMD.ADD_PERSON);
        param.put("company", addPersonRequest.getCompany());
        param.put("icNum", addPersonRequest.getIcNum());
        param.put("jobNum", addPersonRequest.getJobNum());
        // 如果特征为不为空，那么只传特征，不用传图片，以此减少终端的工作
        if (!StringUtils.isEmpty(addPersonRequest.getFaceFeature())) {
            param.put("faceFeature", addPersonRequest.getFaceFeature());
        } else {
            if (StringUtils.isNotBlank(addPersonRequest.getFaceFeatureImgPath())) {
                param.put("faceFeatureImgPath", IpAddressUtil.getImageAddIp(addPersonRequest.getFaceFeatureImgPath()));
            }
        }
        if (!StringUtils.isEmpty(addPersonRequest.getFingerVeinFeature())) {
            param.put("fingerVeinFeature", addPersonRequest.getFingerVeinFeature());
        }

        if(!StringUtils.isEmpty(addPersonRequest.getPalmFeature())){
            param.put("palmFeature", addPersonRequest.getPalmFeature());
        }
        param.put("wiegandNum", addPersonRequest.getWiegandNum());
        param.put("personnelType", addPersonRequest.getPersonnelType());
        if (StringUtils.isNotEmpty(addPersonRequest.getExpiryTime())) {
            param.put("expiryTime", addPersonRequest.getExpiryTime());
        }
        if (StringUtils.isNotBlank(addPersonRequest.getLockerData())) {
            param.put("lockerData", addPersonRequest.getLockerData());
        }
        param.put("deviceId", Long.parseLong(deviceId));
        param.put("memberOrVisitorDeviceId", addPersonRequest.getMemberOrVisitorDeviceId());
        param.put("memberType", addPersonRequest.getMemberType());
        String req = JSON.toJSONString(param);
        gateway.sendToMqtt(MessageFormat.format(Topic.PERSON_TOPIC, deviceId), 1, req);
        logService.addDeviceLog(addPersonRequest.getName(), deviceBase.getName(), req, CMD.ADD_PERSON, param.get(CMD.SERIAL_NUMBER).toString());
        return null;
    }


    @Override
    public ReturnJson updatePerson(String deviceId, UpdatePersonRequest updatePersonRequest) throws Exception {
        DeviceBase deviceBase = deviceBaseService.getById(deviceId);
        Map<String, Object> param = SnowIdGen.getBaseMap();
        param.put("key", "ABCD1234");
        param.put("personnelId", updatePersonRequest.getPersonnelId());
        param.put("name", updatePersonRequest.getName());
        param.put("idCardNum", updatePersonRequest.getIdCardNum());
        param.put("company", updatePersonRequest.getCompany());
        param.put("icNum", updatePersonRequest.getIcNum());
        param.put("jobNum", updatePersonRequest.getJobNum());
        param.put(CMD.CMD, CMD.UPDATE_PERSON);
        // 如果特征为不为空，那么只传特征，不用传图片，以此减少终端的工作

        if (StringUtils.isNotBlank(updatePersonRequest.getFaceFeatureImgPath())) {
            param.put("faceFeatureImgPath", IpAddressUtil.getImageAddIp(updatePersonRequest.getFaceFeatureImgPath()));
        }
        if(!StringUtils.isEmpty(updatePersonRequest.getPalmFeature())){
            param.put("palmFeature", updatePersonRequest.getPalmFeature());
        }
        param.put("wiegandNum", updatePersonRequest.getWiegandNum());
        param.put("deviceId", Long.parseLong(deviceId));
        param.put("memberOrVisitorDeviceId", updatePersonRequest.getMemberOrVisitorDeviceId());
        param.put("memberType", updatePersonRequest.getMemberType());
        if (StringUtils.isNotBlank(updatePersonRequest.getPersonnelType())) {
            param.put("personnelType", updatePersonRequest.getPersonnelType());
        }
        if (StringUtils.isNotBlank(updatePersonRequest.getLockerData())) {
            param.put("lockerData", updatePersonRequest.getLockerData());
        }
        if (StringUtils.isNotEmpty(updatePersonRequest.getExpiryTime())) {
            param.put("expiryTime", updatePersonRequest.getExpiryTime());
        }
        String req = JSON.toJSONString(param);
        gateway.sendToMqtt(MessageFormat.format(Topic.PERSON_TOPIC, deviceId), 1, req);
        logService.addDeviceLog(updatePersonRequest.getName(), deviceBase.getName(), req, CMD.UPDATE_PERSON, param.get(CMD.SERIAL_NUMBER).toString());
        return null;
    }

    @Override
    public GetPersonResponse getPerson(String uri, GetPersonRequest getPersonRequest) throws Exception {
        throw new ServiceException(500, "该版本暂时不支持此操作");

    }

    @Override
    public GetPersonResponse listPerson(String uri, GetPersonRequest request) throws Exception {
        throw new ServiceException(500, "该版本暂时不支持此操作");
    }

    @Override
    public BaseResponse setUserPwd(String uri, SetUserPwdRequest setUserPwdRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setFactoryDefault(String uri, SetFactoryDefaultRequest setFactoryDefaultRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSysTime(String uri, SetSysTimeRequest setSysTimeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse openDoor(String uri, OpenDoorRequest openDoorRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetGeneralParamRequest getGeneralParam(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setGeneralParam(String uri, GetAndSetGeneralParamRequest getAndSetGeneralParamRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse rebootDevice(String uri, RebootDeviceRequest rebootDeviceRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetDoorCondition getDoorCondition(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setDoorCondition(String uri, GetAndSetDoorCondition getAndSetDoorCondition, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public GetAndSetSoundRequest getSound(String uri, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSound(String uri, GetAndSetSoundRequest getAndSetSoundRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }

    @Override
    public BaseResponse setSubscribe(String uri, SetSubscribeRequest setSubscribeRequest, RequestHeader requestHeader) throws Exception {
        return null;
    }
}
